package com.smart.stock.common.util.h2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

public class ColumnTypeUtil {

	public static String java2h2(String columnType) {
		List<String> charList = Arrays.asList("String","Byte","Character","Bool");
		List<String> numList = Arrays.asList("Integer","Short","Long","Float","Double");
		List<String> lobList = Arrays.asList("CLOB","BLOB","NCLOB","BFILE","RAW","LONG RAW");
		List<String> dateList = Arrays.asList("Date","LocalDateTime","LocalDate","LocalTime","Timestamp");

		if (numList.contains(columnType)) {
			columnType = "DOUBLE";
		}else if (dateList.contains(columnType)) {
			columnType = "DATETIME";
		}else if (lobList.contains(columnType)) {
			columnType = "TEXT";
		}else if (charList.contains(columnType)) {
			columnType = "VARCHAR";
		}else {

		}
		return columnType;
	}

	public static String oracle2mysqlMeta(String columnType) {
		List<String> charList = Arrays.asList("VARCHAR2","CHAR","NCHAR","NVARCHAR2");
		List<String> numList = Arrays.asList("NUMBER","FLOAT","REAL","LONG","DECIMAL");
		List<String> lobList = Arrays.asList("CLOB","BLOB","NCLOB","BFILE","RAW","LONG RAW");
		List<String> dateList = Arrays.asList("DATE","TIMESTAMP");

		if (numList.contains(columnType)) {
			columnType = "DOUBLE";
		}else if (dateList.contains(columnType)) {
			columnType = "DATETIME";
		}else if (lobList.contains(columnType)) {
			columnType = "TEXT";
		}else if (charList.contains(columnType)) {
			columnType = "VARCHAR";
		}else {

		}
		return columnType;
	}

	public static String mysql2kingbase(String columnType) {
		List<String> charList = Arrays.asList("VARCHAR2","CHAR","NCHAR","NVARCHAR2");
		List<String> numList = Arrays.asList("INT","INTEGER","MEDIUMINT","LONG","DECIMAL");
		List<String> lobList = Arrays.asList("CLOB","BLOB","NCLOB","BFILE","RAW","LONG RAW");
		List<String> dateList = Arrays.asList("DATE","TIMESTAMP");

		String mapJson = "{'INT':'int4'}";
		JSONObject parse = (JSONObject)JSON.parse(mapJson);
		Object o = parse.get(columnType);

		if (numList.contains(columnType)) {
			columnType = "int4";
		}else if (dateList.contains(columnType)) {
			columnType = "DATETIME";
		}else if (lobList.contains(columnType)) {
			columnType = "TEXT";
		}else if (charList.contains(columnType)) {
			columnType = "VARCHAR";
		}else {

		}
		return columnType;
	}

	public static Object oracle2mysqlData(Object columnValue,String columnType) {
		if (columnValue == null) {
			return columnValue;
		}

		if (columnType.equals("VARCHAR2")||columnType.equals("CLOB")) {
			columnValue = "'"+columnValue+"'";
		}else if (columnType.equals("DATE")) {
			columnValue = "'"+columnValue+"'";
		}else {

		}
		return columnValue;
	}


	/**自动放大字段长度
	 * 200长度以内的，长度放大2倍：processColumn(size,200,2)
	 * @param size
	 * @param maxSize
	 * @param multipleNumber
	 */
	public static Object processColumn(Object size,int maxSize,int multipleNumber){
		Object targetSize = null;
		if (Integer.parseInt(size.toString())<maxSize){
			targetSize = Integer.parseInt(size.toString())*multipleNumber;
		}else{
			targetSize = size;
		}

		return targetSize;
	}

	/**
	 * 200长度以内的，长度放大2倍
	 * @param size
	 * @return
	 */
	public static Object doubleSize(Object size){
		return processColumn(size,200,2);
	}


		public static void main(String[] args) {
		System.out.println(oracle2mysqlMeta("NVARCHAR2"));
	}
}
